const { axios, getToken } = require("./weix");
var {
  weix: { wechatId },
} = require("../config");

/**
 * 返回发送文本消息的xml 字符串
 * @param {String} openid 用户的openid
 * @param {String} content 发送的内容
 * @returns {String}
 */
exports.respondText = function (openid, content) {
  return `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[${content}]]></Content>
  </xml>`;
};

/**
 * 返回发送录音消息的xml 字符串
 * @param {String} openid 用户的openid
 * @param {String} mediaid 通过素材管理中的接口上传多媒体文件，得到的id
 * @returns {String}
 */
exports.respondPic = function (openid, mediaid) {
  return `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[image]]></MsgType>
    <Image>
      <MediaId><![CDATA[${mediaid}]]></MediaId>
    </Image>
  </xml>`;
};

/**
 * 返回发送视频消息的xml 字符串
 * @param {String} openid 用户的openid
 * @param {String} mediaid 通过素材管理中的接口上传多媒体文件，得到的id
 * @returns {String}
 */
exports.respondVoice = function (openid, mediaid) {
  return `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[voice]]></MsgType>
    <Voice>
      <MediaId><![CDATA[${mediaid}]]></MediaId>
    </Voice>
  </xml>`;
};

/**
 * 返回发送视频消息的xml 字符串
 * @param {String} openid 用户的openid
 * @param {String} mediaid 通过素材管理中的接口上传多媒体文件，得到的id
 * @param {String} title 视频消息的标题
 * @param {String} description 视频消息的描述
 * @returns {String}
 */
exports.respondVideo = function (openid, mediaid, title, description) {
  return `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[video]]></MsgType>
    <Video>
      <MediaId><![CDATA[${mediaid}]]></MediaId>
      <Title><![CDATA[${title}]]></Title>
      <Description><![CDATA[${description}]]></Description>
    </Video>
  </xml>`;
};

/**
 * 返回发送音乐消息的xml 字符串
 * @param {String} openid 用户的openId
 * @param {String} mediaid 缩略图的媒体id，通过素材管理中的接口上传多媒体文件，得到的id
 * @param {String} musicUrl 音乐链接
 * @param {String} hqMusicUrl 高质量音乐链接，WIFI环境优先使用该链接播放音乐
 * @param {String} title 音乐标题
 * @param {String} description 音乐描述
 * @returns {String}
 */
exports.respondMusic = function (
  openid,
  mediaid,
  musicUrl,
  hqMusicUrl,
  title,
  description
) {
  return `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[music]]></MsgType>
    <Music>
      <Title><![CDATA[${title}]]></Title>
      <HQMusicUrl><![CDATA[${hqMusicUrl}]]></HQMusicUrl>
      <MusicUrl><![CDATA[${musicUrl}]]></MusicUrl>
      <Description><![CDATA[${description}]]></Description>
      <ThumbMediaId><![CDATA[${mediaid}]]></ThumbMediaId>
    </Music>
  </xml>`;
};

/**
 * 返回发送图文消息集合 字符串
 * @param {String} openid 用户的openId
 * @param {[Object]} messages 土温消息集合
 * @param {String} messages.title 消息标题
 * @param {String} messages.description 图文消息描述
 * @param {String} messages.picur 图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200
 * @param {String} messages.url 点击图文消息跳转链接
 * @returns {String}
 */
exports.respondArticles = function (openid, messages) {
  return (
    `<xml>
    <ToUserName><![CDATA[${openid}]]></ToUserName>
    <FromUserName><![CDATA[${wechatId}]]></FromUserName>
    <CreateTime>${(new Date().getTime() / 1000).toFixed(0)}</CreateTime>
    <MsgType><![CDATA[news]]></MsgType>
    <ArticleCount>${messages.length}</ArticleCount>
    <Articles>` +
    messages
      .map(function (message) {
        return `<item>
        <Title><![CDATA[${message.title}]]></Title>
        <Description><![CDATA[${message.description}]]></Description>
        <PicUrl><![CDATA[${message.picurl}]]></PicUrl>
        <Url><![CDATA[${message.url}]]></Url>
      </item>`;
      })
      .join("") +
    `</Articles>
  </xml>`
  );
};

exports.messageToAlluser = async function (id, content) {
  return axios
    .post("cgi-bin/message/mass/sendall?access_token=" + (await getToken()), {
      filter: {
        is_to_all: false,
        tag_id: id,
      },
      mpnews: {
        media_id: content,
      },
      msgtype: "mpnews",
      send_ignore_reprint: 0,
    })
    .then((res) => {
      if (!res.data.errcode) {
        return res.data;
      } else {
        throw Error(`getAllUserOpenid(): ${res.data.errmsg}`);
      }
    });
};

exports.previewToUser = async function (id, content) {
  return axios
    .post("cgi-bin/message/mass/preview?access_token=" + (await getToken()), {
      // touser: id,
      // text: {
      //   content: content,
      // },
      // msgtype: "text",

      touser: id,
      mpnews: {
        media_id: content,
      },
      msgtype: "mpnews",
    })
    .then((res) => {
      if (!res.data.errcode) {
        return res.data;
      } else {
        throw Error(`getAllUserOpenid(): ${res.data.errmsg}`);
      }
    });
};
